package stack_queue;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 栈和队列
 * User: 王圆豪
 * Date: 2024-07-28
 * Time: 21:07
 */
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack(int[] elem) {
        this.elem = new int[10];
    }

    public void push(int date) {
        if (isFull()) {
            elem = Arrays.copyOf(elem, 2 * usedSize);
        }
        elem[usedSize++] = date;
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    public int pop() {
        try {
            if (isEmpty()){
                throw new EmptyException("栈为空");
            }
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return elem[--usedSize];
    }

    public int peek() {
        try {
            if (isEmpty()){
                throw new EmptyException("栈为空");
            }
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return elem[usedSize - 1];
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
}
